

class TarzanScene extends PhyScene {
    private _bg: TitleMap

    private _lookAtX: number = 0
    private _lookAtY: number = 0

    private _ball: Box2D.Dynamics.b2Body = null
    private _randomMap: RandomTerrien = null

    private _gameOver: boolean
    constructor() {
        super()
        this._gameOver = false
    }

    public init(): void {
        console.log("TarzanScene.init" + App.StageUtils.getHeight() + " " + App.StageUtils.getWidth())
        this._bg = new TitleMap()
        this._bg.initMap()
        this._bg.LookAt(0, 0)
        LayerManager.Game_Bg.addChild(this._bg)

        this.initPhy(this._bg)

        {
            let bodyDef = new Box2D.Dynamics.b2BodyDef()
            bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody
            bodyDef.bullet = true
            bodyDef.linearDamping = 0
            bodyDef.angularDamping = 0
            bodyDef.fixedRotation = false
            bodyDef.allowSleep = true
            bodyDef.position.x = 0
            bodyDef.position.y = 0 //App.StageUtils.getHeight() / 2 / this._p2m
            bodyDef.angle = Math.PI / 2

            let fixtureDef = new Box2D.Dynamics.b2FixtureDef()
            fixtureDef.density = 1
            fixtureDef.friction = 0
            fixtureDef.restitution = 0.8

            this._ball = this._world.CreateBody(bodyDef)
            this._ball.ApplyImpulse(new Box2D.Common.Math.b2Vec2(10, 0), this._ball.GetPosition())
            this._ball.SetUserData(PhyObject.Create(PhyObjectType.Player))
            {
                let shape = new Box2D.Collision.Shapes.b2CircleShape()
                shape.SetRadius(this.PhyLen(20))
                fixtureDef.shape = shape
                this._ball.CreateFixture(fixtureDef)
            }
        }

        this._randomMap = new RandomTerrien(this, this._ball)

        this.World.SetContactListener(new TarzanContactListener(this))

        {
            /*
            let bodyDef = new Box2D.Dynamics.b2BodyDef()
            bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody
            bodyDef.bullet = true
            bodyDef.linearDamping = 0
            bodyDef.angularDamping = 0
            bodyDef.fixedRotation = false
            bodyDef.allowSleep = true
            bodyDef.position.x = this.PhyLen(-50)
            bodyDef.position.y = this.PhyLen(10) //App.StageUtils.getHeight() / 2 / this._p2m
            bodyDef.angle = Math.PI / 2
            let fixtureDef = new Box2D.Dynamics.b2FixtureDef()
            fixtureDef.density = 1
            fixtureDef.friction = 0
            fixtureDef.restitution = 0.8

            let stone = this._world.CreateBody(bodyDef)
            {
                let shape = new Box2D.Collision.Shapes.b2CircleShape()
                shape.SetRadius(this.PhyLen(20))
                fixtureDef.shape = shape
                stone.CreateFixture(fixtureDef)
            }
            */

            /*
            let joinDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef()
            joinDef.Initialize(stone, this._ball, stone.GetPosition())
            joinDef.enableMotor = true
            this._world.CreateJoint(joinDef)
            */
        }
    }

    public lookAt(x, y: number) {
        this._bg.LookAt(x, y)
        this._randomMap.LookAt(x, y)
    }

    private onTap(e: egret.Event) {
        console.log("onTap")
        this._randomMap.onTap()
    }

    private onTapBegin(e: egret.Event) {
        this._randomMap.makeTheRole()
    }
    private onTapEnd(e: egret.Event) {
        this._randomMap.releaseTheRole()
    }

    public GameOver() {
        console.log("GameOver==========================")
        this._gameOver = true
    }

    public onEnter(): void {
        super.onEnter()

        this.init()

        this.addLayer(LayerManager.Game_Bg)
        this.addLayer(LayerManager.UI_Game)

        LayerManager.UI_Game.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this)
        LayerManager.UI_Game.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTapBegin, this)
        LayerManager.UI_Game.addEventListener(egret.TouchEvent.TOUCH_END, this.onTapEnd, this)
        LayerManager.UI_Game.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTapEnd, this)
        LayerManager.UI_Game.touchEnabled = true
    }

    public enterFrame(time: number) {
        if (this._gameOver) {
            return
        }

        this.phyLoop()
        if (this._ball != null) {
            let pos = this._ball.GetPosition()
            this.lookAt(pos.x * this._p2m, pos.y * this._p2m)
        }
    }
}